package cn.haocxy.jfm.gui;

import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import cn.haocxy.jfm.filelist.FileList;

final class ToolBar {

	private final AnchorPane pane = new AnchorPane();
	
	@SuppressWarnings("unused")
	private final FileList model;
	
	@SuppressWarnings("unused")
	public ToolBar(FileList model) {
		
		this.model = model;
		
		BackgroundFill fill = new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY);
		Background bg = new Background(fill);
		pane.setBackground(bg);
		
		Button prev = new Button("←");
		prev.setShape(new Circle(2.5));
		Button next = new Button("→");
		next.setShape(new Circle(2.5));
		Separator sep = new Separator(Orientation.VERTICAL);
		Button parent = new Button(" ↑ ");
		Button flush = new Button("F5");
		
		HBox hbox = new HBox(5);
		hbox.setPadding(new Insets(5));
		hbox.getChildren().addAll(parent, flush);
		
		TextField text = new TextField();
		
		Button cd = new Button("√");
		
		pane.getChildren().addAll(hbox, text, cd);
		
		AnchorPane.setLeftAnchor(text, 80.0);
		AnchorPane.setTopAnchor(text, 5.0);
		AnchorPane.setRightAnchor(text, 35.0);
		AnchorPane.setBottomAnchor(text, 5.0);
		
		AnchorPane.setRightAnchor(cd, 5.0);
		AnchorPane.setTopAnchor(cd, 5.0);
		AnchorPane.setBottomAnchor(cd, 5.0);
		
		text.setText(model.getCurrentDir().toString());
		
		model.getDirEventHandlers().addHandler(e->{
			text.setText(model.getCurrentDir().toString());
		});
		
		parent.setOnAction(e->{
			model.goParent();
		});
		
		text.addEventHandler(KeyEvent.KEY_RELEASED, e->{
			KeyCode code = e.getCode();
			if (code.equals(KeyCode.ENTER)) {
				model.setDir(text.getText());
			}
		});
		
		text.addEventHandler(MouseEvent.MOUSE_CLICKED, e->{
			text.selectAll();
		});
		
		cd.setOnAction(e->{
			model.setDir(text.getText());
		});
		
		flush.setOnAction(e->{
			model.flush();
		});
	}
	
	public Node toNode() {
		return pane;
	}
}
